package com.dream.myqiyi.widget;

import java.util.ArrayList;
import java.util.List;

import com.dream.myqiyi.widget.PinnedHeaderListView.PinnedHeaderAdapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.TextView;

public abstract class PinnedCategoryAdapter extends BaseAdapter implements PinnedHeaderAdapter, OnScrollListener{
	private List<Category> categories = new ArrayList<Category>();
	private int lastItem = 0; 
	
	public void addCategory(String title, int count, Adapter adapter) {
		categories.add(new Category(title, count, adapter));
	}
	
	public void clearCategories() {
		categories.clear();
	}

	@Override  
    public int getCount() {  
        int total = 0;  
          
        for (Category category : categories) {  
            total += category.getAdapter().getCount() + 1;  
        }  
          
        return total;  
    }  
    @Override  
    public Object getItem(int position) {  
        for (Category category : categories) {  
            if (position == 0) {  
                return category;  
            }  
              
            int size = category.getAdapter().getCount() + 1;  
            if (position < size) {  
                return category.getAdapter().getItem(position-1);  
            }  
            position -= size;  
        }  
          
        return null;  
    }  
    @Override  
    public long getItemId(int position) {  
        return position;  
    }  
    
    public boolean isCategory(int position) {
        for (Category category : categories) {  
            if (position == 0) {  
            	return true;
            }  
            int size = category.getAdapter().getCount()+1;  
            if (position < size) {  
                return false;
            }  
            position -= size;  
        }  
        return true; 
    }
    
    public Category getCurrCategory(int position) {
        for (Category category : categories) {  
            if (position == 0) {  
            	return category;
            }  
            int size = category.getAdapter().getCount()+1;  
            if (position < size) {  
                return category;
            }  
            position -= size;  
        }  
        return null; 
    }
    
    public int getNextCategoryPosition(int position) {
    	int ret = position;
    	int diff = 0;
    	boolean isFind = false;
        for (Category category : categories) {  
            int size = category.getAdapter().getCount()+1;  
            if (position == 0) {  
            	isFind = true;
            	diff = size;
            	break;
            }  
            if (position < size) {  
            	isFind = true;
            	diff = size - position;
            	break;
            }  
            position -= size;  
        }  
        if (isFind) {
        	return ret + diff;
        }
        return -1; 
    }
    
    public Object getChildItem(int position) {  
        for (Category category : categories) {  
            if (position == 0) {  
                return null;  
            }  
              
            int size = category.getAdapter().getCount() + 1;  
            if (position < size) {  
                return category.getAdapter().getItem(position-1);  
            }  
            position -= size;  
        }  
          
        return null;  
    }  

      
    public int getViewTypeCount() {  
        int total = 1;  
          
        for (Category category : categories) {  
            total += category.getAdapter().getViewTypeCount();  
        }  
          
        return total;  
    }  
    public int getItemViewType(int position) {  
        int typeOffset = 1;  
          
        for (Category category : categories) {  
            if (position == 0) {  
                return 0;  
            }  
              
            int size = category.getAdapter().getCount() + 1;  
            if (position < size) {  
                return typeOffset + category.getAdapter().getItemViewType(position - 1);  
            }  
            position -= size;  
              
            typeOffset += category.getAdapter().getViewTypeCount();  
        }  
          
        return 0;  
    }  
    @Override  
    public View getView(int position, View convertView, ViewGroup parent) {  
        int categoryIndex = 0;  
          
        for (Category category : categories) {  
            if (position == 0) { 
                return getTitleView(category, categoryIndex,convertView, parent);  
            }  
            int size = category.getAdapter().getCount()+1;  
            if (position < size) {  
                return category.getAdapter().getView(position - 1, convertView, parent);  
            }  
            position -= size;  
              
            categoryIndex++;  
        }  
          
        return null;  
    }  
    
    @Override  
    public int getPinnedHeaderState(int position) {  
    	int realPosition = position;
		if (realPosition < 0) {
			return PINNED_HEADER_GONE;
		}
		int nextSectionPosition = getNextCategoryPosition(position);//得到下一个分组的位置
		if (nextSectionPosition != -1
				&& realPosition == nextSectionPosition - 1) {
			return PINNED_HEADER_PUSHED_UP;
		}
		return PINNED_HEADER_VISIBLE;
    }  
    @Override  
    public void configurePinnedHeader(View header, int position, int alpha) {  
        // TODO Auto-generated method stub  
        if (lastItem != position) {  
            notifyDataSetChanged();  
        }  
        
        int categoryIndex = 0;  
        
        for (Category category : categories) {  
        	int size = category.getAdapter().getCount()+1;  
            if (position < size) { 
                getTitleView(category, categoryIndex, header, null);  
            }  
            
            position -= size;  
              
            categoryIndex++;  
        }  

        lastItem = position;  
    }  
    
    @Override  
    public void onScroll(AbsListView view, int firstVisibleItem,  
            int visibleItemCount, int totalItemCount) {  
        if (view instanceof PinnedHeaderListView) {  
            ((PinnedHeaderListView) view).configureHeaderView(firstVisibleItem);  
        }  
    }  
  
    @Override  
    public void onScrollStateChanged(AbsListView view, int scrollState) {  
  
    } 
    
    @Override  
    public boolean isEnabled(int position) {  
    	// 禁止标签项的响应事件
        for (Category category : categories) {  
            if (position == 0) {  
            	return false;
            }  
            int size = category.getAdapter().getCount()+1;  
            if (position < size) {  
                return super.isEnabled(position);
            }  
            position -= size;  
        }  
        return false; 
    }
      
    protected abstract View getTitleView(Category category,int index,View convertView, ViewGroup parent);  
      
}
